<?php

namespace app\common\service;

use app\common\job\SubscriptionMessage;
use app\common\model\Member;
use think\facade\Queue;

/**
 * 公众号模板消息
 * Class SubscriptionMessageService
 * @package app\common\service
 */
class SubscriptionMessageService
{

    /**
     * TODO 罚款
     * @param $memberId
     * @param $content
     * @param $money
     * @param $orderNo
     * @param $time
     * @return void
     */
    public function messageMargin($memberId, $content, $money, $orderNo, $time)
    {

        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        // TODO
        $template_id = 'MCExqkyuZnPVPyW1UK6ZGEjB1KkRNROGCBbJOAnRQyM';

        $first = $content;
        $keyword1 = $money;
        $keyword2 = $orderNo;
        $keyword3 = date('Y年m月d日', $time);
        $remark = '点击确认或提出异议';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'keyword3' => ['value' => $keyword3, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $url = 'https://m.xiongmaolvshi.cn/punish/' . $memberId;

        $this->pushMessage($touser, $template_id, $data, '', $url);
    }

    /**
     * 律师匹配到免费咨询
     * @param $memberId
     * @param $userName
     * @param $ask
     * @param $time
     * @param $expTime
     * @param string $pagepath
     * @return void
     */
    public function messageFreeConsulting($memberId, $userName, $ask, $time, $expTime, string $pagepath = 'pages/lawyer/lawyer')
    {
        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'J9X_5xaJFFBJsc5KgDkBMjUO0yy_BPKfSaSyITLsddE';

        $first = $userName . '咨询法律问题';
        $keyword1 = '请尽快解答。如您未在' . bcdiv($expTime, 60, 0) . '分钟内答复，客户可改签律师。本推送通知将失效。';
        $keyword2 = $ask;
        $keyword3 = date('Y年m月d日 H:i:s', $time);
        $remark = '点击回复咨询';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'keyword3' => ['value' => $keyword3, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $this->pushMessage($touser, $template_id, $data, $pagepath);

    }

    /**
     * 律师接到到付费咨询服务
     * @param $memberId
     * @param $serverName
     * @param $payMoney
     * @param $nickname
     * @param $time
     * @return void
     */
    public function messagePaidConsulting($memberId, $serverName, $payMoney, $nickname, $time, $pagepath = 'pages/lawyer/lawyer')
    {

        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'LvXgnnuRZUEIkSReA4oSFr3OLLm3ckw2qAkz5cNbvnc';

        $first = '您有新的付费法律服务待处理';
        $keyword1 = $serverName;
        $keyword2 = $payMoney;
        $keyword3 = $nickname;
        $keyword4 = date('Y年m月d日 H:i:s', $time);
        $remark = '点击查询详细咨询信息';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'keyword3' => ['value' => $keyword3, 'color' => '#173177'],
            'keyword4' => ['value' => $keyword4, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $this->pushMessage($touser, $template_id, $data, $pagepath);
    }

    /**
     * 用户追问
     * @param $memberId
     * @param $userName
     * @param $ask
     * @param $content
     * @param $time
     * @return void
     */
    public function messageAsk($memberId, $userName, $ask, $content, $time, string $pagepath = 'pages/lawyer/lawyer')
    {

        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'J9X_5xaJFFBJsc5KgDkBMjUO0yy_BPKfSaSyITLsddE';

        $first = $userName . '咨询法律问题';
        $keyword1 = $ask;
        $keyword2 = $content;
        $keyword3 = date('Y年m月d日 H:i:s', $time);
        $remark = '点击回复咨询';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'keyword3' => ['value' => $keyword3, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $this->pushMessage($touser, $template_id, $data, $pagepath);
    }

    /**
     * 律师回复
     * @param $memberId
     * @param $lawyerName
     * @param $answerContent
     * @param $time
     * @return void
     */
    public function messageLawyerReply($memberId, $lawyerName, $answerContent, $time)
    {
        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'GioAnS2Z1ZGiBCe6-TNbOqCaVsdhWPMDMrMllXVZ3rQ';

        $first = $lawyerName . '律师已回复您的问题';
        $keyword1 = date('Y年m月d日 H:i:s', $time);
        $keyword2 = $answerContent;
        $remark = '点击查看详细回复信息';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $pagepath = 'pages/lawyer/lawyer';

        $this->pushMessage($touser, $template_id, $data, $pagepath);
    }

    /**
     * 新案源
     * @param $memberId
     * @param $source_type
     * @param $content
     * @param $address
     * @param $time
     * @param $sourceId
     * @return void
     */
    public function messageNewSource($memberId, $source_type, $content, $address, $time, $sourceId)
    {

        $touser = $this->getOpenidByMemberId($memberId);
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'MCExqkyuZnPVPyW1UK6ZGEjB1KkRNROGCBbJOAnRQyM';

        $first = '您好，您有一个新的案源';
        $keyword1 = $source_type;
        $keyword2 = $content;
        $keyword3 = $address;
        $keyword4 = date('Y年m月d日', $time);
        $remark = '点击立即抢购';

        $data = [
            'first' => ['value' => $first, 'color' => '#173177'],
            'keyword1' => ['value' => $keyword1, 'color' => '#173177'],
            'keyword2' => ['value' => $keyword2, 'color' => '#173177'],
            'keyword3' => ['value' => $keyword3, 'color' => '#173177'],
            'keyword4' => ['value' => $keyword4, 'color' => '#173177'],
            'remark' => ['value' => $remark, 'color' => '#173177'],
        ];

        $pagepath = 'pages/lawyer/lawyer?source_id=' . $sourceId;

        $this->pushMessage($touser, $template_id, $data, $pagepath);
    }

    private function getOpenidByMemberId($memberId): ?string
    {

        return Member::where('id', $memberId)->where('followedWechat', 1)->value('openidWechat');
    }

    private function pushMessage($touser, $template_id, $data, $pagepath, $url = '')
    {
        $job_data = [
            'touser' => $touser,
            'template_id' => $template_id,
            'data' => $data,
            'pagepath' => $pagepath,
            'url' => $url,
        ];

        Queue::push(SubscriptionMessage::class, $job_data);
    }
}